package com.sunchangming.rpcgen;

public abstract class TypeCollection extends Type {
	@Override
	public String getMarshalStr(Variable v, String osname) {
		final StringBuilder sb = new StringBuilder();
		sb.append("com.tianshen.message.MessageDecoder.writeRawVarint32(").append(osname).append(",")
				.append(v.getName()).append(".size());\n");
		sb.append("for(").append(v.getValueType().getBoxName()).append(" v:").append(v.getName()).append("){\n");
		sb.append(v.getValueType().getMarshalStr(new Variable("v", v.getValueType()), osname));
		sb.append(";\n}");
		return sb.toString();
	}

	@Override
	public String getUnmarshalStr(Variable v, String osname, boolean useThis) {
		final StringBuilder sb = new StringBuilder();
		sb.append("this." + v.getName() + "=new " + this.getName(v) + "();\n");
		sb.append("for(int i=com.tianshen.message.MessageDecoder.readRawVarint32(").append(osname)
				.append(");i!=0;--i){\n");
		final Variable lv = new Variable("_v_", v.getValueType());
		sb.append(lv.getMainType().getBoxName()).append(" ").append(lv.getLocalUnmarshalStr()).append(";\n");
		sb.append("this.").append(v.getName()).append(".add(_v_);\n");
		sb.append("}");
		return sb.toString();
	}

	@Override
	public String getDefaultValue(Variable variable) {
		return "new " + getName(variable) + "()";
	}

	@Override
	public String getHashCodeStr(Variable variable) {
		return "this." + variable.getName() + ".hashCode()";
	}

	@Override
	public String getFromSqlStr(Variable variable, String string) {
		return "throw new RuntimeException(\"Notimpl\")";
	}
}
